package com.llmm.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llmm.eduService.entity.EduSubject;
import com.llmm.eduService.entity.EduSubjectTree;
import com.llmm.eduService.mapper.EduSubjectMapper;
import com.llmm.eduService.service.EduSubjectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EduSubjectServiceImpl implements EduSubjectService {
    @Resource
    private EduSubjectMapper eduSubjectMapper;

    //查询所有课程
    @Override
    public List<EduSubject> findAllSubjects() {
        return eduSubjectMapper.selectList(null);
    }

    //按树形结构查询所有课程
    @Override
    public List<EduSubjectTree> findAllTrees() {
        return eduSubjectMapper.findAllTrees();
    }

    //查询所有一级分类
    @Override
    public List<EduSubject> findAllRoot() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> list = eduSubjectMapper.selectList(wrapper);
        return list;
    }

    //查询所有一级分类的二级分类的个数
    @Override
    public Map findChart() {
        //分类
        HashMap<String, Integer> map = new HashMap<>();
        String title=null;
        Integer count =null;
        //获取所有的一级分类
        List<EduSubject> list = eduSubjectMapper.selectList(new QueryWrapper<EduSubject>().eq("parent_id", "0"));
        for (EduSubject eduSubject : list) {
            title = eduSubject.getTitle();
            // map 中的 value
            count = eduSubjectMapper.selectCount(new QueryWrapper<EduSubject>().eq("parent_id", eduSubject.getId()));
            // 存入集合
            map.put(title,count);
        }
        return map;
    }

    //根据一级分类查询二级分类
    @Override
    public List<EduSubject> findLeafByRoot(String parentId) {
        List<EduSubject> list = eduSubjectMapper.selectList(new QueryWrapper<EduSubject>().eq("parent_id", parentId));
        return list;
    }
}
